package com.isoft.jms.queue;

import javax.jms.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Configuration
public class QueueProducer {
	
	@Value("${spring.activemq.open}")
	private boolean open;
	
	@Autowired
    private Queue queue;
	
	/**
	 * 也可以注入JmsTemplates,JmsMessagingTemplate对JmsTemplates进行了封装
	 */
	@Autowired  
	private JmsMessagingTemplate jmsTemplates;
	
	@JmsListener(destination="out.queue")
	public void consumerMessage(String text){
		System.out.println("从out.queue队列收到的回复消息为:"+text);
	}

	/**
	 * 每3s执行1次
	 */
	@Scheduled(fixedDelay=3000)
    public void send() {
		if (!open)
			return;
       this.jmsTemplates.convertAndSend(this.queue, "天王盖地虎");
       System.out.println("Producer发送的消息为:"+"天王盖地虎");
    }
}
